package com.sz.app.oss.api.logistics.base;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.base.service.ShipService;
import com.sz.biz.logistics.base.dto.ShipDto;
import com.sz.common.base.dto.I18nDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.service.I18nBaseService;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Function: ShipController <br>
 * Author: wentao.chang <br>
 * Date: 2017-08-04 15:19:00
 */
@RestController
@RequestMapping("${api}/biz/ships")
@Api(description = " ", tags = "026-01、船舶管理")
public class ShipController extends BaseController {
    @Autowired
    private ShipService shipService;
    @Autowired
    private I18nBaseService i18nService;
    @Value("${i18n.biz.ship}")
    private String i18nTableName;

    @ApiOperation(value = "添加船舶", notes = "添加船舶, <br>权限:" + Permissions.BIZ_MGT_BASE_TRANSPORT_SHIP)
    @RequestMapping(value = "/", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRANSPORT_SHIP)
    public ResultDto createShipModel(@RequestBody @ApiParam(value = "船舶", required = true) ShipDto model) {
        Integer id = shipService.save(model);
        i18nService.save(id, model.getI18n(), i18nTableName);
        return this.getSaveSuccessDto(MessagesCodes.MODULE_SHIP);
    }

    @ApiOperation(value = "修改船舶", notes = "修改船舶<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_SHIP)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "船舶Id", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "model", value = "船舶", required = true, dataType = "ShipDto", paramType = "body")
    })
    @RequestMapping(value = "/{id}", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRANSPORT_SHIP)
    public ResultDto updateShipModel(@PathVariable("id") int id, @RequestBody ShipDto model) {
        shipService.update(id, model);
        i18nService.save(id, model.getI18n(), i18nTableName);
        return this.getUpdateSuccessDto(MessagesCodes.MODULE_SHIP);
    }

    @ApiOperation(value = "批量删除船舶", notes = "批量删除船舶<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_SHIP)
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRANSPORT_SHIP)
    public ResultDto deleteShipModels(@RequestBody @ApiParam(value = "要删除的id集合", required = true) List<Integer> ids) {
        shipService.deleteByIds(ids);
        return this.getDeleteSuccessDto(MessagesCodes.MODULE_SHIP);
    }

    @ApiOperation(value = "获取船舶", notes = "获取船舶<br>权限：")
    @ApiImplicitParam(name = "id", value = "船舶Id", required = true, dataType = "int", paramType = "path")
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    public ResultDto getShipModel(@PathVariable("id") int id) {
        ShipDto dto = shipService.findById(id);
        return new ResultDto(dto);
    }

    @ApiOperation(value = "检查船舶英文名是否存在", notes = "检查船舶英文名是否存在<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_SHIP)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nameen", value = "nameen", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/checknameen", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRANSPORT_SHIP)
    public ResultDto checkNameEnExist(@RequestParam(required = false) String nameen,
                                    @RequestParam(required = false) Integer id) {
        shipService.checkShipNameEn(nameen, id);
        return new ResultDto();
    }

    @ApiOperation(value = "检查船舶imo是否存在", notes = "检查船舶imo是否存在<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_SHIP)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imo", value = "imo", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/checkimo", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRANSPORT_SHIP)
    public ResultDto checkImoExist(@RequestParam(required = false) String imo,
                                    @RequestParam(required = false) Integer id) {
        shipService.checkImo(imo, id);
        return new ResultDto();
    }
    @ApiOperation(value = "检查船舶mmsi是否存在", notes = "检查船舶mmsi是否存在<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_SHIP)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mmsi", value = "mmsi", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/checkmmsi", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRANSPORT_SHIP)
    public ResultDto checkMmsiExist(@RequestParam(required = false) String mmsi,
                                    @RequestParam(required = false) Integer id) {
        shipService.checkShipMMSI(mmsi, id);
        return new ResultDto();
    }
    @ApiOperation(value = "获取船舶", notes = "获取船舶<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "countryId", value = "国家Id", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "companyId", value = "船公司Id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "升序", dataType = "boolean", paramType = "query")
    })
    @RequestMapping(value = "/", method = {RequestMethod.GET})
    public ResultDto getShipModels(@RequestParam(required = false) String q,
                                   @RequestParam(required = false) String countryId,
                                   @RequestParam(required = false) Integer companyId,
                                   @RequestParam(defaultValue = "1") int pageIndex,
                                   @RequestParam(defaultValue = "20") int pageSize,
                                   @RequestParam(required = false) String sort,
                                   @RequestParam(defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        return shipService.findShipListPage(new QueryParams(pageSize, pageIndex, q, sort, asc)
                .put("countryId", countryId)
                .put("companyId", companyId));
    }

    @ApiOperation(value = "获取船舶短接口", notes = "获取船舶短接口<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/short", method = {RequestMethod.GET})
    public ResultDto getShipModels(@RequestParam(required = false) String q,
                                   @RequestParam(defaultValue = "1") int pageIndex,
                                   @RequestParam(defaultValue = "20") int pageSize) {
        q = StringUtils.replaceSqlPattern(q);
        return shipService.findShipShortListPage(new QueryParams(pageSize, pageIndex, q, null, true));
    }

    @ApiOperation(value = "获取船舶(i18n)", notes = "获取船舶<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language", value = "语言代码", dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "q", value = "查询关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "unfilled", value = "是否未添加国际化字段，true，代表未添加，fasle与null代表所有",
                    dataType = "boolean", paramType = "query", defaultValue = "false"),
            @ApiImplicitParam(name = "sort", value = "排序字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "升序", dataType = "boolean", paramType = "query")
    })
    @RequestMapping(value = "/i18n/{language}", method = {RequestMethod.GET})
    public ResultDto getShipModelsWithI18n(@PathVariable String language,
                                           @RequestParam(required = false) String q,
                                           @RequestParam(defaultValue = "1") int pageIndex,
                                           @RequestParam(defaultValue = "20") int pageSize,
                                           @RequestParam(defaultValue = "false") Boolean unfilled,
                                           @RequestParam(required = false) String sort,
                                           @RequestParam(defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        return shipService.findI18nByParam(new QueryParams(pageSize, pageIndex, q, sort, asc, language).put("unfilled", unfilled));
    }

    @ApiOperation(value = "保存船舶国际化(i18n)", notes = "保存船舶国际化信息<br>权限：" + Permissions.BIZ_MGT_BASE_TRANSPORT_SHIP)
    @RequestMapping(value = "/i18n", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRANSPORT_SHIP)
    public ResultDto setShipModelI18n(@RequestBody @ApiParam(value = "要保存的对象集合", required = true)
                                              List<I18nDto> models) {
        i18nService.save(models, i18nTableName);
        shipService.refreshCache();
        return this.getSaveI18nSuccessDto();
    }
}
